// Utility.cpp

#include "stdafx.h"
#include "Utility.h"
#include "../zlib/gzip.h"

#pragma comment(lib, "zlib.lib")

using namespace zlib;

SimpleRand::SimpleRand()
{
	Seed();
};

void SimpleRand::Seed()
{
	m_iHold=::GetTickCount();
};

void SimpleRand::Seed(int p_iSeed)
{
	m_iHold=p_iSeed;
};

int SimpleRand::Rand()
{
	return( ((m_iHold=m_iHold* 214013L+ 2531011L)>>16)&0x7fff );
};

ThreadLock::ThreadLock()
{
	::InitializeCriticalSectionAndSpinCount((CRITICAL_SECTION*)(&m_xDummy),4000);
};

ThreadLock::~ThreadLock()
{
	::DeleteCriticalSection((CRITICAL_SECTION*)(&m_xDummy));
};

void ThreadLock::Lock()
{
	::EnterCriticalSection((CRITICAL_SECTION*)(&m_xDummy));
};

void ThreadLock::Unlock()
{
	::LeaveCriticalSection((CRITICAL_SECTION*)(&m_xDummy));
};

char** xString::Split(char token, char* input, int& p_count)
{
	int iLength = strlen(input);
	int iNumWords = 0;

	for (int i = 0; i < iLength; i++)
	{
		if (input[i] == token)
		{
			iNumWords++;
		}
	}

	char** output = new char*[iNumWords+1];

	int iter = 0;
	int count = 0;

	for (int i = 0; i < iLength; i++)
	{
		if (input[i] == token)
		{
			int iTempSize = i - iter;
			output[count] = new char[1024];
			for (int j = 0; j < iTempSize; j++)
			{
				output[count][j] = input[iter+j];

				int iL=strlen(output[count]);
				while((iL>0)&&(output[count][iL-1]<=NULL)){iL--;};
				output[count][iL]='\0';
			}
			count++;
			iter = i+1;
		}

		if (count == (iNumWords))
		{
			output[count] = new char[1024];
			int iTempSize = iLength - iter;
			for (int j = 0; j < iTempSize; j++)
			{
				output[count][j] = input[iter+j];

				int iL=strlen(output[count]);
				while((iL>0)&&(output[count][iL-1]<=NULL)){iL--;};
				output[count][iL]='\0';
			}
			break;
		}

	}
		

	p_count = count+1;
	return output;
}

void xString::Replace(char old_token, char new_token, char* p_string)
{
	int iL = strlen(p_string);
	for (int i = 0; i < iL; i++)
	{
		if (p_string[i] == old_token)
		{
			p_string[i] = new_token;
		}
	}
}

std::string xString::getExtension(std::string path)
{
	std::string newString = "";
	if (path.length() > 1)
	{
		for (int i = path.length()-1; i > 0; i--)
		{
			if (path.at(i) != '.')
			{
				newString.insert(0, 1, path.at(i));
			}
			else
			{
				break;
			}
		}
	}
	
	return newString;
}

xFile::xFile()
{

}

xFile::~xFile()
{

}

bool xFile::Open(const char *filePath, int mode)
{
	if (mode == BINARY)
	{
		m_xStream.open(filePath, std::ios::in|std::ios::binary|std::ios::ate);
		if (m_xStream.is_open())
		{
			m_iMode = mode;
			m_cFilePath = filePath;
			return true;
		}
		else
		{
			return false;
		}
	}
	else if (mode == TEXT)
	{
		m_xStream.open(filePath);
		if (m_xStream.is_open())
		{
			m_iMode = mode;
			m_cFilePath = filePath;
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

void xFile::Close()
{
	m_xStream.close();
}

char* xFile::ReadFile()
{
	if (m_iMode == BINARY)
	{
		char* memblock;
		long int size = m_xStream.tellg();
		m_uiFileSize = size;
		memblock = new char [size];
		m_xStream.seekg (0, std::ios::beg);
		m_xStream.read (memblock, size);

		return memblock;
	}
	else if (m_iMode == TEXT)
	{
		//Find out num of characters in file
		int iFileCount = 0;
		while (!m_xStream.eof())
		{
			int x = m_xStream.get();
			iFileCount++;
		}
		m_xStream.close();

		//read the file
		m_xStream.open(m_cFilePath);
		m_xStream.clear();
		char* memblock = new char[iFileCount];
		int i = 0;
		while (!m_xStream.eof())
		{
			m_xStream.get(memblock[i]);
			i++;
		}

		int iL=strlen(memblock);
		while((iL>0)&&(memblock[iL-1]<=NULL)){iL--;};
		memblock[iL]='\0';

		m_uiFileSize = iFileCount;
		return memblock;
	}
	else
	{
		char* memblock = NULL;
		return memblock;
	}
	

}

bool xFile::Exists(const char *filePath)
{
	m_xStream.open(filePath);
	if (m_xStream.is_open())
	{
		m_xStream.close();
		return true;
	}
	else
	{
		return false;
	}

}

xCompress::xCompress()
{

}

xCompress::~xCompress()
{

}

void xCompress::Compress(char *p_cArray)
{
	//CGZip myZipper;
	//myZipper.WriteBuffer(p_cArray, strlen(p_cArray));
}

	



